<?php
class OrderformController extends Controller{
    private $db;

    //构造函数
    public function __init(){
        Global $user;
        //判断是否登陆
        $user = Q('session.user');
        if(empty($user)){
            $_SESSION['gid'] = Q('get.gid');
            header('location:'.u('index/index'));
        }else{
            /*判断登陆信息日志ip*/
            $where = 'uid = '.$user['uid'];
            $loginfo = K('user') -> selclog($where);
            if($loginfo['logip'] != $_SESSION['logip']){
                unset($_SESSION['user']);
                $this -> success($message = '您的账户已从另一个地方登陆！',U('login/login'), $time = 2);
            }
        }
        //向页面传递数据
        $this -> assign('user',$user);
        //指定函数
        $this -> db = k("Orderform");
    }
    public function index(){
        Global $user;
        //获取用户所有订单量
        $where = 'uid ='.$user['uid'];
        $a = $this -> db -> selc_num($where);
        //获取用户未付款
        $where = 'uid ='.$user['uid'].' and otype = 0';
        $a0 = $this -> db -> selc_num($where);
        //获取用户已付款未发货
        $where = 'uid ='.$user['uid'].' and otype = 1';
        $a1 = $this -> db -> selc_num($where);
        //获取用户已发货
        $where = 'uid ='.$user['uid'].' and otype = 2';
        $a2 = $this -> db -> selc_num($where);
        //获取用户已确认收货
        $where = 'uid ='.$user['uid'].' and otype = 3';
        $a3 = $this -> db -> selc_num($where);

        //组合成数组
        $data = array(
            'a' => $a,
            'a0' => $a0,
            'a1' => $a1,
            'a2' => $a2,
            'a3' => $a3,
        );
        //向页面传递数据
        $this -> assign('numdata',$data);

        //显示视图
        $this->display('Template/user/header');
        $this->display('left');
        $this->display();

    }

    public function orderlist(){
        Global $user;
        //获取参数
        $op = Q('get.op','-1');
        $key = Q('get.key','');
        $where = 'uid = '.$user['uid'];

        if(!empty($key)){
            $where .=' and CONCAT(orderlist.omoney,orderlist.notes,orderlist.address,orderlist.adname,orderlist.admoblie) like "%'.$key.'%"';
        }else{
            //获得当前op的条数
            if($op == '-1'){
                $where .= '';
            }else{
                $where .= " and otype =".$op;
            }
        }
        $ordernum = $this -> db -> selc_num($where);

        //翻页参数
        $page = new page($ordernum,10);
        $limit = $page->limit();

        //翻页数据
        $orderinfo = $this -> db -> selc_info($where,$limit);

        //向页面传递数据
        $this -> assign('op',$op);
        $this -> assign('page',$page->show(0));
        $this -> assign('orderinfo',$orderinfo);

        //显示视图
        $this->display('Template/user/header');
        $this->display('left');
        $this->display();
    }
    //添加订单页面
    public function addorder(){
        Global $user;
        $op = Q('get.op','index');

        //判断用户资料是否完整
        if(empty($user['qq']) || empty($user['email']) || empty($user['mobile'])){

            $this -> success($message = '请将资料填写完毕。',U('Info/index'), $time = 2);
        }

        if($op == 'index'){
            //如果传递过来参数
            $gid = Q('get.gid');
            if(!empty($gid)){
                //获取商品详情
                $where = 'gid ='.$gid;
                $goods = K('Goods') -> gone($where);

            }else{
                $this -> success($message = '请选择好商品。',U('index/index/index'), $time = 2);
            }
        }

        if($op == 'modify'){
            //如果传递过来参数
            $oid = Q('get.oid');
            if(!empty($oid)){
                //获取商品详情
                $where = 'oid ='.$oid;
                $goods = $this -> db -> get_one($where);

            }else{
                $this -> success($message = '请选择好商品。',U('index/index/index'), $time = 2);
            }
        }


        //向页面传值
        $this -> assign('op',$op);
        $this -> assign('goods',$goods);
        $this -> assign('vip',$_SESSION['user']['uvip']);
        //显示视图
        $this->display('Template/user/header');
        $this->display('left');
        $this->display();
    }
    //添加订单
    public function add(){
        Global $user;
        $Model = K('Orderform');
        $gid = $_POST['gid'];
        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        $data = array(
            'uid' => $user['uid'],
            'gid' => $gid,
            'onum' => $_POST['onum'],
            'adname'=>$_POST['adname'],
            'admoblie'=>$_POST['admoblie'],
            'address' => $_POST['address'],
            'notes' => $_POST['notes'],
            'omoney' => $_POST['money'],
            'addtime' => time(),
            'uptime' => time(),
            'ordernumber' => array(
                'nid' => '',
            ),
        );
        if($this -> db -> add_order($data)){
            $this -> success($message = '添加成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('添加失败！');
        }
    }
    //修改订单
    public function modify(){
        Global $user;
        $Model = K('Orderform');
        $oid = Q('get.oid');
        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        $data = array(
            'onum' => $_POST['onum'],
            'adname'=>$_POST['adname'],
            'admoblie'=>$_POST['admoblie'],
            'address' => $_POST['address'],
            'notes' => $_POST['notes'],
            'omoney' => $_POST['money'],
            'uptime' => time(),
        );
        $where = 'oid = '.$oid.' and uid='.$user['uid'];
        if($this -> db -> up_order($data,$where)){
            $this -> success($message = '修改成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('修改失败！');
        }
    }
    //交付金额
    public function money(){

        if(isset($_POST['btn'])){
            Global $user;
            $uid = $user['uid'];
            $oid = Q('get.oid');
            $upload = new upload('Upload/mimg/'.date("Y/m/d"));
            $uplofiles = $upload->upload();
            $imgpath = $uplofiles[0]['path'];
            $otype = '1';
            $uptime = time();
            $data = array(
                'oid' => $oid,
                'otype' => $otype,
                'uptime' => $uptime,
                'ordernumber' =>array(
                    'money_pic' => $imgpath,
                    'order_oid' => $oid,
                ),
            );
            $where = 'oid ='.$oid.' and uid = '.$uid;
            if($this -> db -> up_order($data,$where)){
                $this -> success($message = '修改成功。',U('Orderform/orderlist'), $time = 2);
            }else{
                $this -> error('修改失败！');
            }
        }



        //显示视图
        $this->display('Template/user/header');
        $this->display('left');
        $this->display();
    }
    //删除订单
    public function del(){
        Global $user;
        $oid = Q('get.oid');
        $uid = $user['uid'];
        if(empty($oid)){
            $this -> error('删除失败！');
        }
        $where = 'oid = '.$oid.' and uid = '.$uid;
        if($this -> db -> del_order($where)){
            $this -> success($message = '删除成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('删除失败！');
        }
    }
    //确认收货
    public function sure(){
        Global $user;
        $oid = Q('get.oid');
        $uid = $user['uid'];
        $data = array(
            'oid'=>$oid,
            'otype' => '3'
        );
        $where = 'oid = '.$oid.' and uid = '.$uid;
        if($this -> db -> up_order($data,$where)){
            $this -> success($message = '修改成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('修改失败！');
        }
    }

}